package org.nokatag.timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;

public class NTimer {
	
	//--------------间隔时间----------------------------------------------------
	public static final Long TIMER_SECOND=1000L;//以秒为单位
	public static final Long TIMER_MINUTE=TIMER_SECOND*60L;//以分钟为单位
	public static final Long TIMER_HOUR=TIMER_MINUTE*60L;//以小时为单位
	public static final Long TIMER_DAY=TIMER_HOUR*24L;//以天为单位
	public static final Long TIMER_WEEK=TIMER_DAY*7L;//以周为单位
	//--------------固定时间点执行----------------------------------------------
	public static final int TIMER_INTERVAL_SECOND=1;//每几秒钟执行一次
	public static final int TIMER_INTERVAL_MINUTE=2;//每几分钟执行一次
	public static final int TIMER_INTERVAL_HOUR=3;//每几小时执行一次
	public static final int TIMER_INTERVAL_DAY=4;//每几天执行一次
	public static final int TIMER_INTERVAL_WEEK=5;//每几周执行一次
	
	public static final int TIMER_DAY_OF_TIME=6;//每天的几点几分几秒执行,需要的是以小时为单位的时间
	public static final int TIMER_WEEK_OF_TIME=7;//每周的星期几，几点几分几秒执行,星期几，几点几分几秒
	public static final int TIMER_MONTH_OF_TIME=8;//每月的几号,几点几分几秒执行,几号几点几分内秒
	public static final int TIMER_YEAR_OF_TIME=9;//每年的几月，几号,几点几分几秒执行,几月几点几分几秒
	public static final int TIMER_MONTH_MAXDAY_OF_TIME=10;//每个月的最后一天
	//-------------定时器池----------------------------------------------------
	private static NTimer nTimer=null;
	
	public static NTimer IniterNTimerItem(){
		if(nTimer==null)
			return new NTimer();
		else
			return nTimer;
	}
	private NTimer(){
		//clear();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void clear(){
		Iterator iterator=ConstValue.NTIMERITEMS.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String,NTimerItem> map=(Map.Entry<String,NTimerItem>)iterator.next();
			map.getValue().getNtimer().cancel();
		}
		ConstValue.NTIMERITEMS.clear();
	}
	//-------------执行方法体--------------------------------------------------
	private void StartTimer(NTimerItem nt){
			Timer timer=new Timer(true); //建立定时器
			//-----------设置不同的操作----------------------------------
			switch(nt.getDoType()){
			case NTimer.TIMER_INTERVAL_SECOND://秒
				timer.schedule(new NTimerTask(nt),Long.valueOf(nt.getDelayed()), NTimer.TIMER_SECOND*Long.valueOf(nt.getTimerPrave()));//启动
				break;
			case NTimer.TIMER_INTERVAL_MINUTE://分
				timer.schedule(new NTimerTask(nt), Long.valueOf(nt.getDelayed()),NTimer.TIMER_MINUTE*Long.valueOf(nt.getTimerPrave()));//启动
				break;
			case NTimer.TIMER_INTERVAL_HOUR://小时
				timer.schedule(new NTimerTask(nt),Long.valueOf(nt.getDelayed()), NTimer.TIMER_HOUR*Long.valueOf(nt.getTimerPrave()));//启动
				break;
			case NTimer.TIMER_INTERVAL_DAY://日
				timer.schedule(new NTimerTask(nt),Long.valueOf(nt.getDelayed()), NTimer.TIMER_DAY*Long.valueOf(nt.getTimerPrave()));//启动
				break;
			case NTimer.TIMER_INTERVAL_WEEK://周
				timer.schedule(new NTimerTask(nt), Long.valueOf(nt.getDelayed()),NTimer.TIMER_WEEK*Long.valueOf(nt.getTimerPrave()));//启动
				break;
			case NTimer.TIMER_DAY_OF_TIME://每天,08:23:32
				timer.schedule(new NTimerTask(nt), MinuteToDate(nt.getTimerPrave()),NTimer.TIMER_DAY);//每天的几点
				break;
			case NTimer.TIMER_WEEK_OF_TIME://每周,1,08:23:32 表示星期一 的08:23:32 
				timer.schedule(new NTimerTask(nt), WeekToDate(nt.getStart(),nt.getTimerPrave()),NTimer.TIMER_WEEK);//每周的几点
				break;
			//---------------OK--------------------------------------------------
			case NTimer.TIMER_MONTH_OF_TIME://每月,01,08:23:32 表示星期一 的08:23:32 
				timer.schedule(new NTimerTask(nt), MonthToDate(nt.getStart(),nt.getTimerPrave()),NTimer.TIMER_DAY*28);//每月最短28天
				nt.setStart(AddDate(nt.getStart(),Calendar.MONTH,1));//月号加一
				break;
			case NTimer.TIMER_YEAR_OF_TIME://每年,05-01,08:23:32 表示星期一 的08:23:32 
				timer.schedule(new NTimerTask(nt), YearToDate(nt.getStart(),nt.getTimerPrave()),NTimer.TIMER_DAY*365);//每年
				nt.setStart(AddDate(nt.getStart(),Calendar.YEAR,1));//年号加一
				break;
			case NTimer.TIMER_MONTH_MAXDAY_OF_TIME://每月最后一天,08:23:32 表示星期一 的08:23:32 
				timer.schedule(new NTimerTask(nt), MonthMaxDay(nt.getStart(),nt.getTimerPrave()),NTimer.TIMER_DAY*28);//每月最短28天
				nt.setStart(AddDate(nt.getStart(),Calendar.MONTH,1));//月号加一
				break;
			}
			nt.setNtimer(timer);//绑定定时器
			ConstValue.NTIMERITEMS.put(nt.getTimerID(),nt);//进入定时器池
	}
	//-----------------------------------------------------------------------------------------------------
	//----------添加定时器---------------------------------------------------
	public void addTimer(NTimerItem nTimerItem){
		if(getNTimerItem(nTimerItem.getTimerID())==null)
			StartTimer(nTimerItem);
	}
	//----------删除定时器----------------------------------------------------
	public void dellTimer(List<String> TimerID){
		if(!ConstValue.NTIMERITEMS.isEmpty()){
			for(String timerID:TimerID){
				dellTimer(timerID);
			}
		}
	}
	public void dellTimer(String TimerID){
		getNTimerItem(TimerID).getNtimer().cancel();
		if(!ConstValue.NTIMERITEMS.isEmpty()){
			ConstValue.NTIMERITEMS.remove(TimerID);//返回对像
		}
	}
	//-----------修改定时器--------------------------------------------------
	public void updateTimer(List<NTimerItem> nTimerItems){
		for(NTimerItem nt:nTimerItems){
			updateTimer(nt);
		}
	}
	public void updateTimer(NTimerItem NTimerItem){
		dellTimer(NTimerItem.getTimerID());//删除原有的
		addTimer(NTimerItem);//添加一个新的对像
	}
	public NTimerItem getNTimerItem(String NTimerID){
		if(!ConstValue.NTIMERITEMS.isEmpty()){
			return ConstValue.NTIMERITEMS.get(NTimerID);//返回对像
		}
		return null;
	}
	//-----------------------------------------------------------------------------------------------------
	/**
	 * 添加时间
	 */
	public static Date AddDate(Date date,int type,int s){
		Calendar cal=Calendar.getInstance();
		cal.clear();
		cal.setTime(date);
		cal.set(type,cal.get(type)+s);
		return cal.getTime();
	}
	/**
	 * 根据月，时间组合成日期 01,08:23:32  
	 * @param minute
	 * @return
	 */
	public static Date MonthMaxDay(Date date,String minute){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(date)+" "+minute;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Calendar cal=Calendar.getInstance();
		cal.clear();
		try{
			cal.setTime(de.parse(da));
			int   lastday   =   cal.getMaximum(Calendar.MONTH);
			cal.set(Calendar.DATE,lastday); 
			return cal.getTime();
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//------------------------------------------------------------------------------------------------------
	/**
	 * 根据月，时间组合成日期 01,08:23:32  
	 * @param minute
	 * @return
	 */
	public static Date YearToDate(Date date,String Month_day_minute){
		//--------------取得开始年号------------------------------------
		String day="";
		String month="";
		String minute="";
		if(Month_day_minute.indexOf(",")!=-1){
			String month_day=Month_day_minute.split(",")[0];
			minute=Month_day_minute.split(",")[1];
			if(month_day!=null && month_day.indexOf("-")!=-1){
				day=month_day.split("-")[1];
				month=month_day.split("-")[0];
			}
		}else{
			return null;
		}
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(date)+" "+minute;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Calendar cal=Calendar.getInstance();
		cal.clear();
		try{
			cal.setTime(de.parse(da));
			cal.set(Calendar.MONTH,Integer.valueOf(month));//月
			cal.set(Calendar.DAY_OF_MONTH,Integer.valueOf(day));//日
			return cal.getTime();
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//------------------------------------------------------------------------------------------------------
	/**
	 * 根据月，时间组合成日期 01,08:23:32  
	 * @param minute
	 * @return
	 */
	public static Date MonthToDate(Date date,String day_minute){
		String day="";
		String minute="";
		if(day_minute.indexOf(",")!=-1){
			day=day_minute.split(",")[0];
			minute=day_minute.split(",")[1];
		}else{
			return null;
		}
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(date)+" "+minute;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Calendar cal=Calendar.getInstance();
		cal.clear();
		try{
			cal.setTime(de.parse(da));
			cal.set(Calendar.DAY_OF_MONTH,Integer.valueOf(day));
			return cal.getTime();
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//------------------------------------------------------------------------------------------------------
	/**
	 * 根据周，时间组合成日期
	 * @param minute
	 * @return
	 */
	public static Date WeekToDate(Date date,String week_minute){
		String week="";
		String minute="";
		if(week_minute.indexOf(",")!=-1){
			week=week_minute.split(",")[0];
			minute=week_minute.split(",")[1];
		}else{
			return null;
		}
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(date)+" "+minute;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Calendar cal=Calendar.getInstance();
		cal.clear();
		try{
			cal.setTime(de.parse(da));
			cal.set(Calendar.DAY_OF_WEEK,Integer.valueOf(week));
			return cal.getTime();
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//-----------------------------------------------------------------------------------------------------
	/**
	 * 将时间和当天日期组合成日期时间格式
	 * @param minute
	 * @return
	 */
	public static Date MinuteToDate(String minute){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(new Date())+" "+minute;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		try{
			return de.parse(da);
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//----------------------------------------------------------------------------------------------------
	/**
	 * 提前几分钟
	 * @param minute
	 * @param time
	 * @return
	 */
	public static Date AdvanceMinute(Integer minute,String time){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
		String da=df.format(new Date())+" "+time;
		SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		Calendar cal=Calendar.getInstance();
		cal.clear();
		try{
			cal.setTime(de.parse(da));
			cal.add(Calendar.MINUTE, -minute);
			return cal.getTime();
		}catch(Exception se){
			se.printStackTrace();
		}
		return null;
	}
	//==============================================================================
	public static void main(String[] args){
		//SimpleDateFormat de=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
		//System.out.println(de.format(YearToDate("21-23,12:30:00"),0));
		
	}
}
